package org.centsuse.intention_spring.invention.entities.contexts;

import lombok.ToString;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ToString
public class ContextManager {

    /** sessionId: DialogueContext, 管理所有会话的管理器 */
    private final Map<String, DialogueContext> contexts = new ConcurrentHashMap<>();

    /**
     * @description 获取指定会话上下文
     * @param sessionId 会话id
     * @return DialogueContext
     */
    public DialogueContext getContext(String sessionId) {
        return contexts.computeIfAbsent(sessionId, id -> new CommonContext(sessionId));
    }

    /**
     * @description 添加指定会话上下文
     * @param sessionId 会话id
     * @param context 会话上下文
     * @return DialogueContext
     */
    public DialogueContext getContext(String sessionId, DialogueContext context) {
        contexts.putIfAbsent(sessionId, context);
        return contexts.get(sessionId);
    }
}
